package eteam.aps.systemservice.config;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import eteam.aps.common.entity.ParentEntity;
import eteam.aps.systeminterface.model.entity.MailEntity;
import eteam.aps.systemservice.dal.mapper.IMailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

/**
 * @author LiuLei
 */
@Configuration
public class MailConfig {

    @Bean
    public JavaMailSender javaMailSender(@Autowired IMailMapper mapper) {
        // 从数据库查询邮箱信息
        MailEntity mailEntity = mapper.selectOne(new LambdaQueryWrapper<MailEntity>()
                .select(MailEntity::getFUsername, MailEntity::getFPassword, MailEntity::getFHost)
                .ne(MailEntity::getfState, ParentEntity.FSTATE_DELETED).last("LIMIT 0,1"));
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setHost(mailEntity.getFHost());
        javaMailSender.setUsername(mailEntity.getFUsername());
        javaMailSender.setPassword(mailEntity.getFPassword());
        Properties properties = new Properties();
        properties.put("mail.smtp.ssl.enable", true);
        javaMailSender.setJavaMailProperties(properties);
        return javaMailSender;
    }

}
